<?php

namespace App\Store\Controllers\Common;

use App\Admin\Models\SiteCompany;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Common;
use App\Common\Services\Upload;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\AccountRole;
use App\Store\Services\AccountMenuRole;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class CommonController extends Controller
{
    /**
     * 上传文件至OSS
     */
    public function uploadtooss(Request $request)
    {
        $res = Upload::uploadToOss($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path'], 'filename' => $res['filename']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }


    /**
     * 上传文件,常用于上传后下载、或者文件导入以及生成
     */
    public function uploadfiletolocal(Request $request)
    {
        $res = Upload::uploadFile($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }

    /**
     * 获取省市区列表
     */
    public function getregion(Request $request)
    {
        $pid = $request->pid ?? 0;
        $list = Common::getRegion($pid);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /**
     * 获取管理以及门店相关人员的菜单信息
     */
    public function getmenulist(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);

        if ($accountinfo['is_super'] == 1) {

            $menulist = Redis::get('account_user_menulist_Account_0');
            if (!$menulist) {
                AccountMenuRole::setAccountRole(0, 9);
                $menulist = Redis::get('account_user_menulist_Account_0');
            }
            $menulist = json_decode($menulist, true);
        } else {
            $menulist = Redis::get('account_user_menulist_Account_' . $accountinfo['id']);
            if (!empty($menulist)) {
                $menulist = json_decode($menulist, true);
            } else {
                $rule_info = AccountRole::where(['account_id' => $accountinfo['id']])->first();
                $menulist = json_decode($rule_info->menu_ruleinfo, true);
                Redis::set('account_user_menulist_Account_' . $accountinfo['id'], $rule_info->menu_ruleinfo);
            }

            foreach ($menulist as $key => $val) {
                if ($val['is_checked'] == 0) {
                    unset($menulist[$key]);
                } else {
                    if (isset($val['children'])) {
                        foreach ($val['children'] as $kkey => $vval) {
                            if ($vval['is_checked'] == 0) {
                                unset($menulist[$key]['children'][$kkey]);
                            } else {

                                if (isset($vval['children'])) {
                                    foreach ($vval['children'] as $kkkey => $vvval) {
                                        if ($vvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['children'][$kkkey]);
                                        } else {
                                            if (isset($vvval['mate']['buttonList'])) {
                                                foreach ($vvval['mate']['buttonList'] as $bbbkey => $bbbvval) {
                                                    if ($bbbvval['is_checked'] == 0) {
                                                        unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList'][$bbbkey]);
                                                    }
                                                }
                                                $menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList'] = array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList']);
                                            }
                                        }

                                    }
                                    $menulist[$key]['children'][$kkey]['children'] = array_values($menulist[$key]['children'][$kkey]['children']);
                                }


                                if (isset($vval['mate']['buttonList'])) {
                                    foreach ($vval['mate']['buttonList'] as $bbkey => $bbvval) {
                                        if ($bbvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['mate']['buttonList'][$bbkey]);
                                        }
                                    }
                                    $menulist[$key]['children'][$kkey]['mate']['buttonList'] = array_values($menulist[$key]['children'][$kkey]['mate']['buttonList']);
                                }
                            }

                        }
                        $menulist[$key]['children'] = array_values($menulist[$key]['children']);
                    }

                    if (isset($val['mate']['buttonList'])) {
                        foreach ($val['mate']['buttonList'] as $bkey => $bvval) {
                            if ($bvval['mate']['is_checked'] == 0) {
                                unset($menulist[$key]['mate']['buttonList'][$bkey]);
                            }
                        }
                        $menulist[$key]['mate']['buttonList'] = array_values($menulist[$key]['mate']['buttonList']);
                    }

                }
            }
        }

        //是否开通财务表1是2否
        $is_finance = SiteCompany::where(['id' => $accountinfo['cp_id']])->value('is_finance');
        if ($is_finance == 2) {//动态配置菜单

            foreach ($menulist as $mkey => $menuval) {
                if (isset($menuval['path']) && $menuval['path'] == '/manageForm') {

                    foreach ($menuval['children'] as $submkey => $submenuval) {
                        if (isset($submenuval['path']) && $submenuval['path'] == '/finance') {
                            unset($menulist[$mkey]['children'][$submkey]);
                        }

                    }
                    $menulist[$mkey]['children'] = array_values($menulist[$mkey]['children']);


                }
            }
        }


        $menulist = array_values($menulist);


        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);

    }


    public function dolock(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        if ($accountinfo['type'] == 9) {
            $is_locked = $request->is_locked == 2 ? 2 : 1;
            $cp_id = $accountinfo['cp_id'];
            // $is_locked=SiteCompany::where(['id'=>$cp_id])->value('is_locked');
            SiteCompany::where(['id' => $cp_id])->update(['is_locked' => $is_locked]);
            Redis::set('LOCKINFO_' . $cp_id, $is_locked);


            return response()->json(['code' => '000', 'msg' => '操作成功']);
        } else {
            return response()->json(['code' => '001', 'msg' => '暂无权限']);
        }
    }


    /**
     * 文件下载
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download(Request $request)
    {

        $filename = $request->filename;
        $filePath = storage_path('app/download/' . $filename);
        if (file_exists($filePath)) {
            return response()->download($filePath, $filename);
        }
        return response()->json(['code' => '001', 'msg' => '文件不存在']);
    }
}
